﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TravelAgency.DAL;
using System.Web.Security;

namespace TravelAgency.AspNet.Users
{
    public partial class MakeRegistration : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string tripIdString = this.Request.QueryString["tripId"];
            int tripId = int.Parse(tripIdString);
            Trip requestedTrip;
            TravelAgencyDAO dao = new TravelAgencyDAO();
            using (dao)
            {
                requestedTrip = dao.GetTripByTripId(tripId);
            }

            this.Session["tripId"] = requestedTrip.TripId;

            this.RangeValidatorPeopleNumber.MaximumValue = 
                (requestedTrip.MaxPeopleCapacity - requestedTrip.CurrentPeopleCapacity).ToString();

            this.FillTripDate(requestedTrip);
        }

        private void FillTripDate(Trip selectedTrip)
        {
            if (selectedTrip != null)
            {
                this.PanelTrip.Visible = true;

                int tripCapacity = selectedTrip.MaxPeopleCapacity;
                int tripCurrentCalacity = selectedTrip.CurrentPeopleCapacity;
                int availableCapacity = tripCapacity - tripCurrentCalacity;

                if (availableCapacity > 0)
                {
                    this.LabelTripNotAvailable.Visible = false;
                    this.PanelTripDetails.Visible = true;
                    this.LabelTitle.Text = Server.HtmlEncode(selectedTrip.Title);
                    this.LabelDescription.Text = Server.HtmlEncode(selectedTrip.Description);
                    this.LabelStartDate.Text = string.Format("{0:yyyy.MMM.dd}", selectedTrip.StartDate);
                    this.LabelEndDate.Text = string.Format("{0:yyyy.MMM.dd}", selectedTrip.EndDate);
                    this.LabelPrice.Text = string.Format("{0:c}", selectedTrip.Price);
                    this.LabelCapacity.Text = availableCapacity.ToString();
                }
                else
                {
                    this.LabelTripNotAvailable.Visible = true;
                    this.PanelTripDetails.Visible = false;
                }
            }
        }

        protected void LinkButtonReservation_Click(object sender, EventArgs e)
        {
            Page.Validate();

            if (Page.IsValid)
            {
                if (this.Session["tripId"] != null)
                {
                    string peopleCountString = this.TextBoxPeopleNumber.Text;
                    int peopleCounte = int.Parse(peopleCountString);
                    MembershipUser currentUser = Membership.GetUser();
                    Guid userId = (Guid)currentUser.ProviderUserKey;

                    TravelAgencyEntities dbContext = new TravelAgencyEntities();
                    using (dbContext)
                    {
                        try
                        {
                            int tripId = (int)this.Session["tripId"];
                            Trip trip = dbContext.Trips.First(t => t.TripId == tripId);
                            trip.CurrentPeopleCapacity += peopleCounte;
                            Reservation reservation = new Reservation();
                            reservation.UserId = userId;
                            reservation.PeopleCount = peopleCounte;
                            reservation.TripId = trip.TripId;
                            dbContext.Add(reservation);
                            dbContext.SaveChanges();
                            this.Response.Redirect("~/Users/Trips.aspx");
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                }
            }
        }

        protected void LinkButtonCancelReservation_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("~/Users/Trips.aspx");
        }
    }
}